﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace HdipCricketClub.Models
{
    [MetadataType(typeof(OpponentOtherFixtureDetailMetaData))]
    public partial class OpponentOtherFixtureDetail
    {
        CricketClubEntities db = new CricketClubEntities();

        OpponentFixturePlayer opponentFixturePlayer = new OpponentFixturePlayer();
        OpponentFixturePlayerRepository opponentFixturePlayerRepository = new OpponentFixturePlayerRepository();

        public List<string> ValidateOpponentOtherDetails(OpponentOtherFixtureDetail opponentOtherDetail)
        {
            string message;

            List<string> errorMessages = new List<string>();

            if (opponentOtherDetail.byes < 0)
            {
                message = "byes must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            if (opponentOtherDetail.leg_byes < 0)
            {
                message = "leg byes must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            if (opponentOtherDetail.wides < 0)
            {
                message = "wides must be greater than or equal to than zero";
                errorMessages.Add(message);
            }

            if (opponentOtherDetail.no_balls < 0)
            {
                message = "no-balls must be greater than or equal to zero";
                errorMessages.Add(message);
            }

            return errorMessages;
        }

        public double GetTotalExtras(OpponentOtherFixtureDetail opponentOtherDetail)
        {
            double totalExtras = (double)opponentOtherDetail.byes + (double)opponentOtherDetail.leg_byes + (double)opponentOtherDetail.wides + (double)opponentOtherDetail.no_balls;

            return totalExtras;
        }

        public void UpdateOpponentOtherDetails(OpponentOtherFixtureDetail opponentOtherDetail, List<OpponentFixturePlayer> opponentBatsmen)
        {
            OpponentOtherFixtureDetail opponentOtherFixtureDetail = new OpponentOtherFixtureDetail();

            opponentOtherFixtureDetail.fixture_id = opponentOtherDetail.fixture_id;
            opponentOtherFixtureDetail.byes = opponentOtherDetail.byes;
            opponentOtherFixtureDetail.leg_byes = opponentOtherDetail.leg_byes;
            opponentOtherFixtureDetail.wides = opponentOtherDetail.wides;
            opponentOtherFixtureDetail.no_balls = opponentOtherDetail.no_balls;
            opponentOtherFixtureDetail.total_runs = opponentFixturePlayer.GetTotalBatsmenRuns(opponentBatsmen) + GetTotalExtras(opponentOtherDetail);
            opponentOtherFixtureDetail.wickets_lost = opponentFixturePlayer.GetTotalWicketsLost(opponentBatsmen);

            db.OpponentOtherFixtureDetails.AddObject(opponentOtherFixtureDetail);
            db.SaveChanges();
        }

        public void EditOpponentOtherFixtureDetail(OpponentOtherFixtureDetail otherDetail)
        {
            OpponentOtherFixtureDetail detailToEdit = db.OpponentOtherFixtureDetails.Where(ofd => ofd.fixture_id == otherDetail.fixture_id).Single();

            detailToEdit.byes = otherDetail.byes;
            detailToEdit.leg_byes = otherDetail.leg_byes;
            detailToEdit.wides = otherDetail.wides;
            detailToEdit.no_balls = otherDetail.no_balls;

            List<OpponentFixturePlayer> fixturePlayers = opponentFixturePlayerRepository.GetOpponentPlayers(otherDetail.fixture_id);
            detailToEdit.total_runs = opponentFixturePlayerRepository.GetTotalBattingRuns(fixturePlayers) + detailToEdit.byes + detailToEdit.leg_byes + detailToEdit.wides + detailToEdit.no_balls;
            detailToEdit.wickets_lost = opponentFixturePlayer.GetTotalWicketsLost(fixturePlayers);

            db.ObjectStateManager.ChangeObjectState(detailToEdit, EntityState.Modified);
            db.SaveChanges();

        }

        public void DeleteOpponentOtherFixtureDetails(int Id)
        {
            OpponentOtherFixtureDetail otherDetails = db.OpponentOtherFixtureDetails.Where(cofd => cofd.fixture_id == Id).Single();

            db.OpponentOtherFixtureDetails.DeleteObject(otherDetails);
            db.SaveChanges();
        }
        
    }
    public class OpponentOtherFixtureDetailMetaData
    {
        [Display(Name = "Byes")]
        public double byes;

        [Display(Name = "Leg Byes")]
        public double leg_byes;

        [Display(Name = "Wides")]
        public double wides;

        [Display(Name = "No Balls")]
        public double no_balls;

    }
}